﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Text.RegularExpressions;
using Lending.RMF.Tool;

namespace AutoRequest
{
    public abstract class IResource
    {
        static Random random = new Random();
        public int total;
        public int recrod;
        public int jumpwitherror;
        public int jumpwithtime;
        public int jumpwithempty;
        public int jumpwithganji;
        public int repeat;
        protected string[] Forbiddens;
        protected string ConnectStr = "";
        protected Dictionary<int, string> dict = new Dictionary<int, string>();
        public IResource(string constr)
        {
            this.SetDict();
            string text = "赶集@";
            if (!string.IsNullOrEmpty(text))
            {
                this.Forbiddens = text.Split(new char[]
				{
					'@'
				});
                this.ConnectStr = constr;
                return;
            }
            throw new Exception("forbiddens配置节为空");
        }
        protected string Pickbody(string html)
        {
            return new Regex("\\<body.*?\\>.*?\\</body\\>").Match(html).Value;
        }
        private decimal GetMinSalary(string salary)
        {
            if (salary.Trim() == "面议" || string.IsNullOrEmpty(salary.Trim()))
            {
                return 0m;
            }
            string value = new Regex("\\d+").Match(salary).Value;
            decimal result = 0m;
            if (decimal.TryParse(value, out result))
            {
                return result;
            }
            return 0m;
        }
        private decimal GetMaxSalary(string salary)
        {
            return 0m;
        }
        private decimal GetSalaryPeriod(string salary)
        {
            if (salary.Contains("小时"))
            {
                return 160m;
            }
            if (salary.Contains("天"))
            {
                return 40m;
            }
            if (salary.Contains("周"))
            {
                return 8m;
            }
            return 1m;
        }
        public abstract void Do();
        protected abstract void SetDict();
        protected abstract void Analysts(string html, string uri, ref int count);
        protected abstract int PositionTypeMapping(string uri);
        protected abstract string PositionNameMapping(string uri);
        public abstract int WorkAreaMapping(string str);
        protected abstract int CompanyBusinessMapping(string orgstr);
        protected abstract int CompanyTypeMapping(string orgstr);
        protected abstract int CompanydimensionsMapping(string orgstr);
        protected abstract int GenderMapping(string gender);
        protected abstract int DegreeMapping(string degree);
        protected abstract int VacancyMapping(string vacancy);
        protected abstract int ExperiencedMapping(string experience);
        protected int PostData(Info item)
        {
            string recruiter = Common.GetRecruiter();
            int result;
            try
            {
                using (SqlConnection sqlConnection = new SqlConnection(this.ConnectStr))
                {
                    sqlConnection.Open();
                    SqlCommand com = sqlConnection.CreateCommand();
                    if (this.IsNotRecord(com, HashAlgorithm.Encrypt(string.Concat(new string[]
					{
						item.Caption, 
						";", 
						item.ContactMan, 
						";", 
						item.ContactPhone
					}), 0)))
                    {
                        int depositionCompanyId = this.PostNewDepositCompany(com, recruiter, item.CompanyName, item.CopAbout, item.CopBusiness, item.CopType, item.CopDimensions);
                        int recruitId = this.PostNewRecruitSheet(com, item.Caption, recruiter, depositionCompanyId, 1, item.ContactMan, item.ContactPhone, item.Address, HashAlgorithm.Encrypt(string.Concat(new string[]
						{
							item.Caption, 
							";", 
							item.ContactMan, 
							";", 
							item.ContactPhone
						}), 0));
                        this.PostNewPosition(com, recruitId, item.PositionName, item.Vacancy, item.PositionType, item.Salry, "不限", item.Degree, item.Experience, item.PositionDescript);
                        LogHelper.Write((CommonLogger)0, (LogLevel)1, "tran.Commit()");
                        sqlConnection.Close();
                        result = 1;
                        return result;
                    }
                    sqlConnection.Close();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(0, 0, ex, "");
            }
            return 0;
            return result;
        }
        private bool IsNotRecord(SqlCommand com, string querycode)
        {
            com.Parameters.Clear();
            com.CommandText = "select 1 from recruitsheet where querycode = @querycode";
            com.Parameters.Add(new SqlParameter("@querycode", querycode));
            object obj = com.ExecuteScalar();
            return obj == null;
        }
        private int PostNewDepositCompany(SqlCommand com, string agent, string companyName, string companyDetail, string bussiness, string copType, string copDim)
        {
            com.Parameters.Clear();
            com.CommandText = "insert into depositcompany(agent,companyname,companydetail,postdate,companybusiness,companytype,companydimensions,templete) values(@agent,@companyname,@companydetail,getdate(),@companybusiness,@companytype,@companydimensions,0) select SCOPE_IDENTITY()";
            com.CommandType = CommandType.Text;
            com.Parameters.Add(new SqlParameter("@agent", agent));
            com.Parameters.Add(new SqlParameter("@companyname", companyName));
            com.Parameters.Add(new SqlParameter("@companydetail", companyDetail));
            com.Parameters.Add(new SqlParameter("@companybusiness", this.CompanyBusinessMapping(bussiness)));
            com.Parameters.Add(new SqlParameter("@companytype", this.CompanyTypeMapping(copType)));
            com.Parameters.Add(new SqlParameter("@companydimensions", this.CompanydimensionsMapping(copDim)));
            object value = com.ExecuteScalar();
            return Convert.ToInt32(value);
        }
        private int PostNewRecruitSheet(SqlCommand com, string caption, string agent, int depositionCompanyId, int recruitType, string Contact, string ContactPhone, string address, string querycode)
        {
            com.Parameters.Clear();
            com.CommandText = "insert into recruitsheet(caption,recruiter,depositcompanyId,recruitType,recruitSubject,contact,contactphone,workarea,workaddress,publishdate,customermanager,templete,isverify,enableemail,querycode,refresh,Source) values(@caption,@recruiter,@depositcompanyId,@recruitType," + (random.Next(0, 100) <= 5 ? "1" : "0") + ",@contact,@contactphone,@workarea,@workaddress,getdate(),null,0,1,0,@querycode,getdate(),-1)select SCOPE_IDENTITY()";
            com.CommandType = CommandType.Text;
            com.Parameters.Add(new SqlParameter("@caption", caption));
            com.Parameters.Add(new SqlParameter("@recruiter", agent));
            com.Parameters.Add(new SqlParameter("@depositcompanyId", depositionCompanyId));
            com.Parameters.Add(new SqlParameter("@recruitType", recruitType));
            com.Parameters.Add(new SqlParameter("@contact", Contact));
            com.Parameters.Add(new SqlParameter("@contactphone", ContactPhone));
            com.Parameters.Add(new SqlParameter("@workarea", this.WorkAreaMapping(address)));
            com.Parameters.Add(new SqlParameter("@workaddress", address));
            com.Parameters.Add(new SqlParameter("@querycode", querycode));
            object value = com.ExecuteScalar();
            return Convert.ToInt32(value);
        }
        private void PostNewPosition(SqlCommand com, int recruitId, string positionName, string vacancy, int positionType, string salary, string gender, string degree, string experienced, string positionDesctipt)
        {
            com.Parameters.Clear();
            com.CommandText = "insert into position(recruitId,positionName,vacancy,positiontype,minsalary,maxsalary,period,gender,degree,experienced,positiondescript,scantime,operatorname,trusteeship,isindex,positionpriority,positionlevel) values(@recruitId,@positionName,@vacancy,@positiontype,@minsalary,@maxsalary,@period,@gender,@degree,@experienced,@positiondescript,0,'',0,0,0,0) select SCOPE_IDENTITY()";
            com.CommandType = CommandType.Text;
            com.Parameters.Add(new SqlParameter("@recruitId", recruitId));
            com.Parameters.Add(new SqlParameter("@positionName", positionName));
            com.Parameters.Add(new SqlParameter("@vacancy", this.VacancyMapping(vacancy)));
            com.Parameters.Add(new SqlParameter("@positiontype", positionType));
            com.Parameters.Add(new SqlParameter("@minsalary", this.GetMinSalary(salary)));
            com.Parameters.Add(new SqlParameter("@maxsalary", this.GetMaxSalary(salary)));
            com.Parameters.Add(new SqlParameter("@period", this.GetSalaryPeriod(salary)));
            com.Parameters.Add(new SqlParameter("@gender", this.GenderMapping(gender)));
            com.Parameters.Add(new SqlParameter("@degree", this.DegreeMapping(degree)));
            com.Parameters.Add(new SqlParameter("@experienced", this.ExperiencedMapping(experienced)));
            com.Parameters.Add(new SqlParameter("@positiondescript", positionDesctipt.Replace("<br/>", "\r\n")));
            com.ExecuteScalar();
        }
    }
}
